package com.sfang.common.base;

import com.google.common.collect.Lists;
import com.sfang.common.base.http.Interceptor.HttpLogInterceptor;
import com.sfang.common.base.http.Interceptor.LogMdcInterceptor;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import java.util.List;

/**
 * web相关配置
 *
 * @author xuzilou
 * @date 2021/3/24 17:09
 */
@Configuration
public class WebConfig implements WebMvcConfigurer {

    /**
     * 排除路径
     */
    private final static List<String> EXCLUDE_PATH_PATTERNS = Lists.newArrayList();

    static {
        EXCLUDE_PATH_PATTERNS.add("/springfox-swagger-ui/**");
        EXCLUDE_PATH_PATTERNS.add("/swagger-ui.html/**");
        EXCLUDE_PATH_PATTERNS.add("/v2/api-docs");
        EXCLUDE_PATH_PATTERNS.add("/resources/**");
        EXCLUDE_PATH_PATTERNS.add("/swagger-resources/**");
        EXCLUDE_PATH_PATTERNS.add("/swagger/**");
    }

    private final HttpLogInterceptor httpLogInterceptor;

    public WebConfig(HttpLogInterceptor httpLogInterceptor, LogMdcInterceptor logMdcInterceptor) {
        this.httpLogInterceptor = httpLogInterceptor;
    }

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(httpLogInterceptor)
                .excludePathPatterns(EXCLUDE_PATH_PATTERNS);
    }

}
